#include "dirtyrect.h"

namespace animationagent {

Uint16 DirtyRect::num = 0;
SDL_Rect DirtyRect::dirty_rects[DIRTYRECT_MAX_NUMBER] = {{0}};

//TODO: bug:if rects have intersection, the 'draw' function will draw those interlaced area more than once.
void DirtyRect::register_to(SDL_Rect *rect)
{
    if (num == DIRTYRECT_MAX_NUMBER) {
        return;
    }
    int i;
    for (i=0; i<num; i++) {
        if (rect->x >= dirty_rects[i].x &&
                rect->x + rect->w <= dirty_rects[i].x + dirty_rects[i].w &&
                rect->y >= dirty_rects[i].y &&
                rect->y + rect->h <= dirty_rects[i].y + dirty_rects[i].h) {
            break;
        }
    }
    if (i == num) {
        dirty_rects[num] = *rect;
        num++;
        printf("reg rect: %d,%d,%d,%d\n", rect->x, rect->y, rect->w, rect->h);
    }
}

void DirtyRect::clear()
{
    num = 0;
}

Uint16 DirtyRect::get_number()
{
    return num;
}

bool DirtyRect::is_all_dirty()
{
    if (num == DIRTYRECT_MAX_NUMBER) {
        return true;
    } else {
        return false;
    }
}

SDL_Rect *DirtyRect::get_rect(int index)
{
    if (index < num) {
        SDL_Rect rect = dirty_rects[index];
        return &(dirty_rects[index]);
    } else {
        return NULL;
    }
}

}

